// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: donggu

#pragma once
#include <experimental/filesystem>
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "coin2/exchange/base/feed/ws/ws_subscriber_util.h"
#include "coin2/exchange/base/market/market_exchange_api.h"
#include "coin2/exchange/feed.pb.h"

using coin2::exchange::feed::WebsocketConfig;

// This represents the specification of exchange.
// This is different from our configuration.
struct FeedSubscriptionSpec {
  coin2::exchange::base::market::MarketExchangeApi mea;
  std::string host;
  std::string port;
  std::string path;
  std::string http_proxy;
  std::map<std::string, WsUrl> extra_urls;
  std::string decompress;
  std::string subscription_req;
  std::string subscription_resp;
  std::string unsubscription_req;
  std::string unsubscription_resp;
  bool set_tlsext_host_name;
  bool support_ping_frame;
  bool handle_ping_msg;
  WsRateLimit authorized_limit;
  WsRateLimit unauthorized_limit;
  std::map<std::string, std::string> channels;
  std::map<std::string, std::vector<std::string>> recipes;
  std::string default_recipe;
  bool use_periodic_resubscribe;
  int64_t resubscribe_interval_ns;
  bool require_auth;
  bool require_header;
  std::string periodic_ping_msg;
  int64_t ping_interval_ns;
  int64_t reconnect_delay_ns;
  int64_t periodic_force_reconnect_minute;
  int symbols_per_connection;
  bool require_regenerate_path;
  bool permessage_deflate;

  static std::unordered_map<MarketExchangeApi, FeedSubscriptionSpec> FromToml(
      const std::experimental::filesystem::path& path);
};


struct WsConnectionConfig {
  int connection_id;
  int worker_id;
  WsUrl url;
  std::string topic;
  std::vector<std::string> symbols;
  char group;
  std::string key_filepath;
  bool require_auth;
  bool require_header;
  // config for whole mea, reuse stuct
  const FeedSubscriptionSpec* mea_config;
  const WebsocketConfig* ws_config;
};
